﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Windows.Forms;
using AccessControl.Business;
using AccessControl.Common.Enumerations;
using AccessControl.Utility.EnumHelper;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;


namespace AccessControl.SecurityMonitoring.Form
{
    public partial class EditPerson : DevExpress.XtraEditors.XtraForm
    {
        #region Field

        private static String _pkPerson;



        #endregion

        #region Singletone Members

        private static EditPerson _editPerson;

        private DateTime _personInfoDate;

        public EditPerson()
        {
            InitializeComponent();
        }

        public void LoadPerson(){
            var a = EnumHelper.ToList(typeof(PersonTitleConstants));
            gridLookUpEditPesonTitle.Properties.DataSource = a;
            var perosnRow = PersonBusiness.SelectByFilter(String.Format("PkPerson='{0}'", _pkPerson)).Single();
            textEditPkPerson.Text = perosnRow.pkPerson;
            textEditPersonFirstName.Text = perosnRow.PersonFirstName;
            textEditPersonLastName.Text = perosnRow.PersonLastName;
            radioGroupPersonIsMale.EditValue = perosnRow.PersonIsMale;
            gridLookUpEditPesonTitle.EditValue = (PersonTitleConstants)perosnRow.PersonTitle;
            memoEditPersonDesc.Text = perosnRow.PersonDesc;
            if (!perosnRow.IsPersonExpireDateNull())
                faDatePickerPersonExpireDate.SelectedDateTime = perosnRow.PersonExpireDate;
            _personInfoDate = perosnRow.PersonInfoDate;
            textEditPersonRefNumber.Text = perosnRow.PersonRefNumber;
            pictureEditPersonPhoto.Image = ByteImageConverter.FromByteArray(perosnRow.PersonPhoto);

        }

        public static EditPerson GetInstance(String pkPerson)
        {
            _pkPerson = pkPerson;
            if ((_editPerson == null) || (_editPerson.IsDisposed))
                _editPerson = new EditPerson();

            _editPerson.LoadPerson();
            return _editPerson;
        }

        #endregion

        #region Event Handler

        private void pictureEditPersonPhoto_Click(object sender, EventArgs e)
        {
            var result = openFileDialog.ShowDialog();
            if (result == DialogResult.OK)
                pictureEditPersonPhoto.Image = Image.FromFile(openFileDialog.FileName);

        }

        private void simpleButtonSubmit_Click(object sender, EventArgs e)
        {
            UpdatePersonToDatabse();

        }

        private void simpleButtonClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion

        #region Private Method

        private bool CheckAllRequeredField()
        {
            errorProvider.Clear();
            var hasError = true;
            if (textEditPkPerson.Text == string.Empty)
            {
                errorProvider.SetError(textEditPkPerson, "شناسه فرد را وارد نمایید");
                hasError = false;
            }

            if (textEditPersonFirstName.Text == string.Empty)
            {
                errorProvider.SetError(textEditPersonFirstName, "نام فرد را وارد نمایید");
                hasError = false;
            }

            if (textEditPersonLastName.Text == string.Empty)
            {
                errorProvider.SetError(textEditPersonLastName, "نام خانوادگی فرد را وارد نمایید");
                hasError = false;
            }

            if (textEditPersonRefNumber.Text == string.Empty)
            {
                errorProvider.SetError(textEditPersonRefNumber, "شماره شناسایی فرد را وارد نمایید");
                hasError = false;
            }
            if (gridLookUpEditPesonTitle.EditValue == null)
            {
                errorProvider.SetError(gridLookUpEditPesonTitle, "عنوان فرد را وارد نمایید");
                hasError = false;
            }

            return hasError;
        }

        private void UpdatePersonToDatabse()
        {
            if (!CheckAllRequeredField())
                return;
            try
            {
                Common.AccessControlDS.tblPersonRow row = new Common.AccessControlDS.tblPersonDataTable().NewtblPersonRow();
                row.pkPerson = textEditPkPerson.Text;
                row.PersonInfoDate = _personInfoDate;
                row.PersonFirstName = textEditPersonFirstName.Text;
                row.PersonLastName = textEditPersonLastName.Text;
                row.PersonIsMale = (bool)radioGroupPersonIsMale.EditValue;
                row.PersonTitle = (Byte)((PersonTitleConstants)gridLookUpEditPesonTitle.EditValue);
                row.PersonDesc = memoEditPersonDesc.Text;
                if (faDatePickerPersonExpireDate.SelectedDateTime.HasValue)
                    row.PersonExpireDate = faDatePickerPersonExpireDate.SelectedDateTime.Value;

                row.PersonRefNumber = textEditPersonRefNumber.Text;
                if (pictureEditPersonPhoto.Image != null)
                    row.PersonPhoto = ByteImageConverter.ToByteArray(pictureEditPersonPhoto.Image, ImageFormat.Jpeg);
                else
                    row.PersonPhoto = new byte[0];

                PersonBusiness.Update(row);

                Close();


            }
            catch (Exception ex){
                XtraMessageBox.Show("خطا در به روز رسانی\n"+ex.Message,Text,MessageBoxButtons.OK,MessageBoxIcon.Error);
            }

        }

        #endregion
    }
}